package com.hourse.app.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.internal.LinkedTreeMap;
import com.hourse.app.entity.ExternalAccountEntity;
import com.hourse.app.service.ExternalAccountService;
import com.hourse.app.service.PlatformAccountService;
import com.hourse.app.service.PlatformOyxService;
import com.hourse.app.vo.rms.TradesRetVo;
import com.hourse.app.vo.web.BalanceRetVo;
import com.hourse.app.vo.web.OrdersRetVo;
import com.hourse.okx.config.APIConfiguration;
import com.hourse.okx.enums.I18nEnum;
import com.hourse.okx.service.account.AccountAPIService;
import com.hourse.okx.service.account.impl.AccountAPIServiceImpl;
import com.hourse.okx.service.marketData.MarketDataAPIService;
import com.hourse.okx.service.marketData.impl.MarketDataAPIServiceImpl;
import com.hourse.okx.service.trade.TradeAPIService;
import com.hourse.okx.service.trade.impl.TradeAPIServiceImpl;
import com.hourse.platform.exception.AppException;
import com.hourse.platform.utils.StringTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @作者 ：lichenguang
 * @描述 ：【平台账户信息】
 * @创建 ：2022/3/21 15:25
 */
@Service
@Slf4j
public class PlatformAccountServiceImpl implements PlatformAccountService {

  @Value("${okx.http.url}")
  private String okxHttpUrl;

  @Autowired
  private ExternalAccountService externalAccountService;

  @Autowired
  private PlatformOyxService platformOyxService;

  /**
   * @描述 ：【查询当前委托订单】
   * @作者 ：lichenguang
   * @时间 ：2022/3/22 16:45
   */
  @Override
  public List<OrdersRetVo> findNowOrders(APIConfiguration config, String currency) throws AppException {

    log.info("查询当前委托订单");
    if (StringTools.isNull(currency)) {
      currency = "";
    }

    return (List<OrdersRetVo>) platformOyxService.getOrdersPending(currency, config);
  }

  /**
   * @描述 ：【查询历史委托订单】
   * @作者 ：lichenguang
   * @时间 ：2022/3/22 16:58
   */
  @Override
  public List<OrdersRetVo> findHistoryOrders(APIConfiguration config, String currency) throws AppException {

    if (StringTools.isNull(currency)) {
      currency = "";
    }

    return (List<OrdersRetVo>) platformOyxService.getOrders(currency, config);

  }

  /**
   * @描述 ：【查询-资产(余额)】
   * @作者 ：lichenguang
   * @时间 ：2022/3/22 17:01
   */
  @Override
  public List<BalanceRetVo> findBalance(APIConfiguration config, String currency) throws AppException {
    log.info("欧易获取账户余额");
    if (StringTools.isNull(currency)) {
      currency = "";
    }

    return (List<BalanceRetVo>) platformOyxService.balance(currency, config);
  }

  /**
   * @描述 ：【获取欧易USDT余额】
   * @作者 ：lichenguang
   * @时间 ：2022/3/25 20:03
   */
  @Override
  public Object getUSDTBalance() {



    return null;
  }

  /**
   * @描述 ：【获取欧易三方配置信息(存在账户认证信息)】
   * @作者 ：lichenguang
   * @时间 ：2022/3/23 10:17
   */
  @Override
  public APIConfiguration getConfig(String userId) throws AppException {

    ExternalAccountEntity externalAccountEntity = externalAccountService.getIsUsePlatform(userId);

    if (externalAccountEntity == null) {
      throw new AppException("该账号没有配置三方信息");
    }

    APIConfiguration config = new APIConfiguration();
    config.setEndpoint(this.okxHttpUrl);
    // apiKey，api注册成功后页面上有
    config.setApiKey(externalAccountEntity.getUKey());
    config.setSecretKey(externalAccountEntity.getUSecret());
    config.setPassphrase(externalAccountEntity.getPassphrase());

    config.setPrint(true);
    config.setI18n(I18nEnum.ENGLISH);

    return config;
  }

  public void toResultString(String flag, Object object) {
    StringBuilder info = new StringBuilder();
    info.append("\n").append("=====>").append(flag).append(":\n").append(JSON.toJSONString(object));
    log.info(info.toString());
  }
}
